From 92eed92a247707c181e61929800604f5c7d7bde0 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 22 Aug 2014 09:53:56 -0700 Subject: [PATCH] Fix `cargo run` for renamed bin targets --- src/cargo/ops/cargo_run.rs | 19 ++++++++++++------ tests/test_cargo_run.rs | 40 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 67a1f7c2f..ea8af8286 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -1,23 +1,30 @@ use std::os; use ops; -use util::{CargoResult, human, process, ProcessError}; +use util::{CargoResult, human, process, ProcessError, Require}; use core::source::Source; use sources::PathSource; pub fn run(manifest_path: &Path, options: &mut ops::CompileOptions, args: &[String]) -> CargoResult> { - if !manifest_path.dir_path().join("src").join("main.rs").exists() { - return Err(human("`src/main.rs` must be present for `cargo run`")) - } - let mut src = try!(PathSource::for_path(&manifest_path.dir_path())); try!(src.update()); let root = try!(src.get_root_package()); + let mut bins = root.get_manifest().get_targets().iter().filter(|a| { + a.is_bin() && a.get_profile().is_compile() + }); + let bin = try!(bins.next().require(|| { + human("a bin target must be available for `cargo run`") + })); + match bins.next() { + Some(..) => return Err(human("`cargo run` requires that a project only \ + have one executable")), + None => {} + } let compile = try!(ops::compile(manifest_path, options)); - let exe = manifest_path.dir_path().join("target").join(root.get_name()); + let exe = manifest_path.dir_path().join("target").join(bin.get_name()); let exe = match exe.path_relative_from(&os::getcwd()) { Some(path) => path, None => exe, diff --git a/tests/test_cargo_run.rs b/tests/test_cargo_run.rs index adc4f0c6e..fae6001e9 100644 --- a/tests/test_cargo_run.rs +++ b/tests/test_cargo_run.rs @@ -79,8 +79,26 @@ test!(no_main_file { assert_that(p.cargo_process("cargo-run"), execs().with_status(101) - .with_stderr("`src/main.rs` must be present for \ - `cargo run`\n")); + .with_stderr("a bin target must be available \ + for `cargo run`\n")); +}) + +test!(too_many_bins { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", "") + .file("src/bin/a.rs", "") + .file("src/bin/b.rs", ""); + + assert_that(p.cargo_process("cargo-run"), + execs().with_status(101) + .with_stderr("`cargo run` requires that a project only \ + have one executable\n")); }) test!(run_dylib_dep { @@ -113,3 +131,21 @@ test!(run_dylib_dep { assert_that(p.cargo_process("cargo-run").arg("hello").arg("world"), execs().with_status(0)); }) + +test!(run_bin_different_name { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + + [[bin]] + name = "bar" + "#) + .file("src/bar.rs", r#" + fn main() { } + "#); + + assert_that(p.cargo_process("cargo-run"), execs().with_status(0)); +}) -- 2.30.2